//
//  NetworkTools.swift
//  weibo19
//
//  Created by Arvin on 15/11/16.
//  Copyright © 2015年 Arvin. All rights reserved.
//

import Foundation
import AFNetworking

private let host = "https://api.weibo.com/"
//private let host = "https://baidu.com/"

private let weiboDomain = "WebKitErrorDomain"

enum HTTPMothed: Int {
    case POST = 0
    case GET = 1
}

class NetworkTools: AFHTTPSessionManager {
    // 单列
    static let sharedTools: NetworkTools = {
        let tools = NetworkTools(baseURL: NSURL(string: host))
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    func requestJSONData(HTTPType: HTTPMothed, urlSting: String, parma: [String: AnyObject], finished: (result: [String: AnyObject]?, error: NSError?) -> ()) {
        
        if HTTPType == .POST {
            POST(urlSting, parameters: parma, success: { (_, result) -> Void in
                // 检验数据是否字典
                if let dict = result as? [String: AnyObject] {
                    finished(result: dict, error: nil)
                    return
                }
                // 自定义错误
                let dataError = NSError(domain: weiboDomain, code: -1000, userInfo: [NSLocalizedDescriptionKey : "数据错误"])
                finished(result: nil, error: dataError)
                
                }) { (_, error) -> Void in
                    finished(result: nil, error: error)
            }
        } else {
            
            GET(urlSting, parameters: parma, success: { (_, result) -> Void in
                if let dict = result as? [String: AnyObject] {
                    finished(result: dict, error: nil)
                    return
                }
                
                let dataError = NSError(domain: weiboDomain, code: -1000, userInfo: [NSLocalizedDescriptionKey : "数据错误"])
                finished(result: nil, error: dataError)
                
                }) { (_, error) -> Void in
                    finished(result: nil, error: error)
            }

        }
    }
    
    // 注意: NSError后面要加问号, 不然会报奇怪的错
    func uploadText(urlStr: String, params: [String: AnyObject],imageData: NSData?, finished: (result: [String: AnyObject]?, error: NSError?) -> ()) {
        
        if imageData == nil {
            // 上传文本
            POST(urlStr, parameters: params, success: { (_, result) -> Void in
                finished(result: result as? [String : AnyObject], error: nil)
                }) { (_, error) -> Void in
                    print(error)
                    finished(result: nil, error: error)
                    
            }
        } else {
        
            // 上传图片和文本
            POST(urlStr, parameters: params, constructingBodyWithBlock: { (multipartFormData) -> Void in
                
                // imageData 二进制图片
                // name: 服务器接收数据的字段
                // fileName: 上传到服务器的保存文件的名字
                // mimeType: 文件类型
                multipartFormData.appendPartWithFileData(imageData!, name: "pic", fileName: "OMG", mimeType: "image/jpeg")
                }, success: { (_, result) -> Void in
                    
                    finished(result: result as? [String : AnyObject], error: nil)
                    
                }) { (_, error) -> Void in
                    finished(result: nil, error: error)
        }
    }
        
    }
}